import { defHttp } from '/@/utils/http/axios';
import { RoleModel } from '/@/api/sys/model/roleModel';

enum RoleApi {
  GetRoleList = '/api/role',
  CreateRole = '/api/role',
  GetRoleDetail = '/api/role/<pk>',
  UpdateRole = '/api/role/<pk>',
  DeleteRole = '/api/role/<pk>',
  GetPermissionTree = '/api/permission/get_permission_tree',
  GetPermissionIdById = '/api/role/<pk>/permissions',
}

export function getRoleList(params: RoleModel | null) {
  return defHttp.get<RoleModel[]>(
    {
      url: RoleApi.GetRoleList,
      params,
    },
    {
      withToken: true,
    },
  );
}

export function addRole(data: RoleModel) {
  return defHttp.post<RoleModel>(
    {
      url: RoleApi.CreateRole,
      data,
    },
    {
      withToken: true,
    },
  );
}

export function getRoleDetail(pk: number) {
  return defHttp.get<RoleModel>(
    {
      url: RoleApi.GetRoleDetail.replace('<pk>', String(pk)),
    },
    {
      withToken: true,
    },
  );
}

export function updateRole(data: RoleModel) {
  return defHttp.put<RoleModel>(
    {
      url: RoleApi.UpdateRole.replace('<pk>', String(data.id)),
      data,
    },
    {
      withToken: true,
    },
  );
}

export function deleteRole(pk: number) {
  return defHttp.delete<RoleModel>(
    {
      url: RoleApi.DeleteRole.replace('<pk>', String(pk)),
    },
    {
      withToken: true,
    },
  );
}

export function getPermissionTree() {
  return defHttp.get(
    {
      url: RoleApi.GetPermissionTree,
    },
    {
      withToken: true,
    },
  );
}

export function getPermissionIds(pk: number) {
  return defHttp.get<{ menus: number[] }>(
    {
      url: RoleApi.GetPermissionIdById.replace('<pk>', String(pk)),
    },
    {
      withToken: true,
    },
  );
}

export function assignPermission(pk: number, menus: number[]) {
  return defHttp.put<{ menus: number[] }>(
    {
      url: RoleApi.GetPermissionIdById.replace('<pk>', String(pk)),
      data: { menus },
    },
    {
      withToken: true,
    },
  );
}
